// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    [NonValidation]
    //[AuthorizePolicy]
    [AllowAnonymous]
    //[MaMenu(code: "DataConfig", name: "配置", area: "HtAdmin",pCode :"SystemManager")]
    // [Route("Dc")]
    //[Route("[area]/Abc/[action]")]
    [MaMvc(area: "HtAdmin", controllerDescription: "数据配置", PCode = "HtAdmin_Developer")]
    public class DataConfigController : HtAdminBaseController
    {
        private readonly ILogger<DataConfigController> _logger;
        private readonly IDataConfigService _dataConfigService;

        public DataConfigController(ILogger<DataConfigController> logger
        , IDataConfigService dataConfigService)
        {
            _logger = logger;
            _dataConfigService = dataConfigService;
        }

        [HttpGet]
        [MaMenu(name: "数据配置")]
        [MaPermission(name: "浏览", actions: "Index,GetList")]
        public async Task<IActionResult> Index(int pid = 0)
        {
            return View();
        }
        [UnifyResult(typeof(DataConfigOut))]
        [HttpGet, HttpPost]
        public async Task<JsonResult> GetList(DataConfigQuery searchquery)
        {
            searchquery.Limit = 9999;
            var pagedlist = await _dataConfigService.GetAllByPageToLayui(searchquery);
            return Json(pagedlist);
        }

        [MaPermission(name: "详情", actions: "Info")]
        public async Task<ActionResult> Info(int? id, int dpid = 0)
        {
            ViewData["Dpid"] = dpid;
            DataConfigOut entity = null;
            if (id.HasValue)
            {
                entity = await _dataConfigService.Get<DataConfigOut>(id.Value);
            }
            return View(entity ?? new DataConfigOut());
        }

        [HttpPost]
        // [ValidateInput(false)]
        [MaPermission(name: "编辑", actions: "Edit")]
        public async Task<JsonResult> Edit(DataConfigInput input)
        {
            await _dataConfigService.InsertOrUpdate(input);
            return Json("");
        }

        [HttpPost]
        [MaPermission(name: "删除", actions: "Delete")]
        public async Task<JsonResult> Delete(int id)
        {
            await _dataConfigService.Delete(id);
            return Json("");
        }

        [HttpPost]
        [MaPermission(name: "设置状态", actions: "SetState")]
        public async Task<JsonResult> SetState(int id, bool state, string type)
        {
            await _dataConfigService.SetState(id, state, type);
            return Json("");
        }

        [MaPermission(name: "获取数据配置树", actions: "GetDcTrees")]
        [UnifyResult(typeof(DataConfigOut))]
        public async Task<JsonResult> GetDcTrees()
        {
            return Json(await _dataConfigService.GetDtree());
        }
    }
}
